/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SensorView;

/**
 *
 * @author thomas.s
 */

import javax.microedition.lcdui.*;
import javax.microedition.sensor.*;
import java.util.*;
import javax.microedition.rms.*;
import sUtils.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;




public class SensorCanvas extends Canvas implements DataListener {

    int th;
    SensorData[] data;
    String[] prop;
    Timer timer;
    int current;
    long lastMouse;
    int vx, vy, mx, my, dy, key=0;
    int timeout;
    Image bg=null;
    
    public SensorCanvas () {
        current = -1;
        th = 32;
        vx = vy = 0;
        data = new SensorData[64];
        prop = new String[64];
        int z=3;
        dy = 0;
        int vol=-1;
        try {
            InputStream is = getClass().getResourceAsStream("/sonar.mp3");
            Player player = Manager.createPlayer(is, "audio/mp3");
            vol=-2;
            player.realize();
            vol=-3;
            VolumeControl vc = (VolumeControl)player.getControl("VolumeControl");
            vol=(vc!=null)?-5:-4;
            vol = vc.getLevel();
            
            player.close();
        }catch(Exception e){  }
        
        prop[z++] = "locale: "+System.getProperty("microedition.locale") + " vol: "+vol;


        //prop[z++] = "language="+System.getProperty("microedition.language");
        timer = new Timer();
        timer.schedule(new updateTask(this), 100, 100 );
    }

    class updateTask extends TimerTask {
        private Canvas canvas;
        updateTask( Canvas c ){  canvas = c;  }
        public void run() {  canvas.repaint(); }
    }

    public void change( int d )
    {
        if (current>0) if (data[current]!=null) data[current].close();
        current+=d; if (current>data.length || data[current]==null) current=0;
        if (current<0) for ( int i=data.length-1; i>=0; i--) if (data[i]!=null) { current=i; break; }
        data[current].connect(this);
    }

    public boolean setSensor( String name, String url ) {

        for ( int i=0; i<data.length; i++ )
            if (data[i]==null)
            {
               data[i] = new SensorData(name, url);
               return true;
            }

        return false;
    }

    public void setTherm( String s )
    {
        int i = Integer.parseInt(s);
        i = (int)((((double)i)*-0.75 + 75.0)*10.0);
        prop[3] = Lang.word("therm: ","Temp.: ") +(i/10)+ ","+(i%10)+"°";
    }


    protected void paint (Graphics g) {
        int w = getWidth ();
        int h = getHeight ();
        prop[0] = Lang.word("memory: ","Speicher: ") + (Runtime.getRuntime().totalMemory()/1024) + " KB";
        prop[1] = Lang.word("free: ","Frei: ") + (Runtime.getRuntime().freeMemory()/1024) + " KB";
        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore("Space",true);
            prop[2] = "RMS: " + (rs.getSizeAvailable()/1024) + " KB";
            rs.closeRecordStore(); } catch( Exception e ) {}
        vy += dy;
        if (vy<-800) { vy=-800; dy=0; } else
            if ( vy>0) { vy=0; dy=0; }

        // Fill the background
        if (bg==null) {
            g.setColor (0x000000);
            g.fillRect (0, 0, w, h);
        } else
            g.drawImage(bg, 0, h-bg.getHeight(), 0);

        

        g.setFont( Font.getFont( Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL ) );
        int y=1;
        for ( int i=0; (i<data.length) && ((th*i)<800); i++ )
            if (data[i]!=null) {
                 if (current==i) g.setColor(0xffff00); 
                 else 
                 { if ((i&1)==1) g.setColor (0xffffff); else g.setColor(0xa0a0a0); }
                 data[i].updateValue();
                 g.drawString (data[i].value(), 1+vx, vy+y, Graphics.LEFT | Graphics.TOP);y+=th;
            }

        for ( int j=0; j<prop.length; j++ )
            if (prop[j]!=null)
            {
                if ((j&1)==1) g.setColor (0xffffff); else g.setColor(0xa0a0a0);
                g.drawString (prop[j], 1+vx, vy+y, Graphics.LEFT | Graphics.TOP); y+=th;
            }

        g.setColor(0xffffff);
        g.drawString( "touch: " + mx+","+my+", key: "+key+ ", res: "+w+"x"+h,1,h-th,0);
    }


    public void keyRepeated (int key) {
        keyPressed (key);
    }

    protected void keyPressed (int k) {
         switch (key=k) {
            case -10:  {
                try { System.gc(); } catch( Exception e) { prop[5]="gc exp"; }
                try { Runtime.getRuntime().gc(); } catch( Exception e) { prop[5]="gc exp"; }
                change(-1);
            } break;
         }
        repaint();
    }

    public void dataReceived( SensorConnection conn, Data[] arg, boolean lost )
    {
        for ( int i=0; i<data.length; i++ )
        if ( data[i]!=null )
            if (data[i].getConn()==conn)
            {
                data[i].setData(arg); break;
            }
    }


    public void pointerPressed( int x, int y )
    {
        lastMouse = System.currentTimeMillis();
        mx = x; my = y;
    }

    public void pointerReleased( int x, int y )
    {
        if ((System.currentTimeMillis()-lastMouse)<240)
        {
            vx=vy=0;
            change(1);
        }
        else vx=0;
    }

    public void pointerDragged( int x, int y )
    {
        if ((System.currentTimeMillis()-lastMouse)>240)
        {
            vx += x-mx; dy += y-my;
            mx=x; my=y;
        }
    }

}
